package com.geek.video.web.controller;

import com.geek.video.enums.*;
import com.geek.video.service.VipAnalysisService;
import com.geek.video.service.dto.VipAnalysisDTO;
import com.geek.video.web.response.ParsingTheUrlRes;
import com.geek.video.web.response.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;


/**
 * vip解析表(VipAnalysis)表控制层
 *
 * @author qinxh
 * @since 2021-01-25 16:33:49
 */
@RestController
@RequestMapping("vipAnalysis/")
public class VipAnalysisController {

    @Autowired
    private VipAnalysisService vipAnalysisService;

    @GetMapping("{id}/")
    public Mono<ResponseData> selectOne(@PathVariable("id") Integer id) {

        return ResponseData.ofSuccess(this.vipAnalysisService.queryById(id));
    }

    @PostMapping("")
    public Mono<ResponseData> save(@RequestBody VipAnalysisDTO dto) {

        if (dto == null || StringUtils.isEmpty(dto.getName()) || StringUtils.isEmpty(dto.getUrl()) || dto.getWay() == null) {
            return ResponseData.ofStatus(ResponseEnum.BIND_ERROR);
        }
        // 默认状态启用
        dto.setState(VipAnalysisStateEnum.ENABLE.getCode());

        return ResponseData.ofSuccess(this.vipAnalysisService.insert(dto));
    }

    /**
     * 解析 url 得到播放信息。测试示例：
     * <p>
     * 优酷：https://v.youku.com/v_show/id_XMjgwNDkwNzE2.html?spm=a2hcb.12701310.app.5~5!2~5!3~5~5~5~5~5~5~A&s=4ce42e368c2e11e0a046
     * <p>
     * 爱奇艺：https://www.iqiyi.com/v_19rra0h3wg.html
     * <p>
     * 腾讯视频：https://v.qq.com/x/cover/1o29ui77e85grdr/h0022ah1yrf.html
     * <p>
     * B站：https://www.bilibili.com/bangumi/play/ss28274/?from=search&seid=16059313263453952350
     *
     * @param url 被解析的 url
     * @return ResponseData
     */
    @GetMapping("analysis/")
    public Mono<ResponseData> parsingTheVip(String url) {

        if (StringUtils.isEmpty(url)) {
            return ResponseData.ofStatus(ResponseEnum.PARAM_EMPTY);
        }
        ParsingTheUrlRes res = vipAnalysisService.parsingTheVip(url);
        if (res != null) {
            return ResponseData.ofSuccess(res);
        }

        return ResponseData.ofStatus(ResponseEnum.NO_PARSE_SOURCE);
    }

}